package com.skin;

import java.io.File;
import java.io.FilenameFilter;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Vector;

import android.content.Context;
import android.util.AndroidException;

public class SkinManager {
	public static final String SKIN_PATH = "";
	public static final FilenameFilter SKIN_FILTER = new FilenameFilter() {
		@Override
		public boolean accept(File dir, String filename) {
			return filename.matches("^.*?\\.skn$");
		}
	};

	private static final String PACKAGEPARSER = "android.content.pm.PackageParser";
	private static final String ASSETMANAGER = "android.content.res.AssetManager";
	private static final String ADDASSETPATH = "addAssetPath";
	private static final String APPLICATIONINFO = "applicationInfo";
	private static Constructor<?> clsPackageParser;
	private static Constructor<?> clsAssetManager;
	private static Method mtdPPParsePackage;
	private static Method mtdAMAddAssetPath;
	private static Field fldPPApplicationInfo;
	static {
		try {// 通过反射初始化相关的类和方法
			Class<?>[] typeArgs;
			Class<?> packageParser = Class.forName(PACKAGEPARSER);
			typeArgs = new Class<?>[1];
			typeArgs[0] = String.class;
			clsPackageParser = packageParser.getConstructor(typeArgs);
			fldPPApplicationInfo = packageParser
					.getDeclaredField(APPLICATIONINFO);

			Class<?> assetManager = Class.forName(ASSETMANAGER);
			typeArgs = new Class<?>[0];
			clsAssetManager = assetManager.getConstructor(typeArgs);
			typeArgs = new Class[1];
			typeArgs[0] = String.class;
			mtdAMAddAssetPath = assetManager.getDeclaredMethod(ADDASSETPATH,
					typeArgs);
		} catch (Exception ex) {
		}
	}

	private static SkinManager mInstance = null;

	public static SkinManager getInstance(Context context)
			throws SkinParseException {
		if (null == mInstance)
			mInstance = new SkinManager(context);
		return mInstance;
	}

	private Context mainContext;

	private SkinManager(Context context) {
		mainContext = context.getApplicationContext();
	}

	public SkinInfo getSkinInfo(String skinName) throws NameNotFoundException {
		// SkinInfo skinInfo = new SkinInfo();
		File skinFile = new File(SKIN_PATH, skinName);
		try {
			Object[] args;
			args = new Object[1];
			args[0] = skinFile.getAbsolutePath();
			Object am = clsAssetManager.newInstance(args);
		} catch (Exception ex) {
		}

		// skinInfo.icon = res.getDrawable(R.drawable.icon);
		// skinInfo.name = res.getString(R.string.name);
		// skinInfo.author = res.getString(R.string.author);
		// res = null;
		return null;
	}

	public SkinInfo[] getSkinsInfo() {
		Vector<SkinInfo> skins = new Vector<SkinInfo>();
		SkinInfo skin;
		File skinDir = new File(SKIN_PATH);
		File[] skinFiles = skinDir.listFiles(SKIN_FILTER);
		for (int i = 0; i < skinFiles.length; i++) {
			try {
				skin = getSkinInfo(skinFiles[i].getName());
				skins.add(skin);
			} catch (NameNotFoundException ex) {
			}
		}
		return (SkinInfo[]) skins.toArray();
	}

	public Skin getCurrent() {
		return null;
	}

	public static class SkinParseException extends AndroidException {

		public SkinParseException() {
		}

		public SkinParseException(String name) {
			super(name);
		}
	}

	public static class NameNotFoundException extends AndroidException {
		private static final long serialVersionUID = 6180858844060179476L;

		public NameNotFoundException() {
		}

		public NameNotFoundException(String name) {
			super(name);
		}
	}

}
